Ext.ns('ISales')


ISalesUI = function(config){
    Ext.apply(this, config); // Applies the stuff we configured the UI with in the form 
    this.init();
};

ISalesUI.prototype = {

    agentPath: 'ag.ExtArc.ApplicationTemplate?Openagent',
    server: ISales.Connections['server'],
    path: ISales.Connections['path'],
    title: 'defaultTitle',
    LanguageCode: '',
    init: function(){
    
    
    
        Ext.QuickTips.init();
        
        if (typeof(ISales.Labels) == 'undefined') {
            //English defaults for global labels in application. 
            ISales.Labels = {
                'home': 'Home',
                'workflowadministration': 'Workflow Administration',
                'menuadministration': 'Menu Administration',
                'extarcadministration': 'Administration',
                'items': 'items'
            }
        }
        
        if (typeof(ISales.ActivityNames) == 'undefined') {
            ISales.ActivityNames = {
                'syMeeting': 'Meeting',
                'syTask': 'Task'
            }
        }
        
        if (typeof(ISales.PluralClassNames) == 'undefined') {
            ISales.PluralClassNames = {
                'clCompanyProfile': 'Companies',
                'clContactProfile': 'Contacts'
            }
        }
        
        this.Toolbar = new ExtArc.Menu.Toolbar({
            style: {
                marginTop: '5px'
            },
            ButtonName: 'Start'
        })
        
        this.tabPanel = new Ext.TabPanel({
        
            plain: true,
            hideBorders: true,
            enableTabScroll: true,
            id: 'isales-tabpanel',
            split: true,
            margins: '15 15 15 15',
            border: false,
            activeTab: 0,
            items: [{
                iconCls: 'Icon_home',
                title: ISales.Labels['home'],
                xtype: 'isalesportallayout1'
            
            }            /*
             {
             
             title: ISales.Labels['workflowadministration'],
             layout: 'fit',
             listeners: {
             'activate': function(){
             if (!Ext.getCmp('WorkflowUI')) {
             this.add(new ExtArc.Workflow.UI({
             id: 'WorkflowUI'
             }));
             }
             }
             
             }
             }, {
             
             title: ISales.Labels['menuadministration'],
             layout: 'fit',
             listeners: {
             'activate': function(){
             if (!Ext.getCmp('MenuUI')) {
             this.add(new ExtArc.Menu.UI({
             id: 'MenuUI'
             }));
             }
             }
             
             }
             }, {
             
             title: ISales.Labels['extarcadministration'],
             layout: 'fit',
             closable: true,
             listeners: {
             'activate': function(){
             if (!Ext.getCmp('AdminUI')) {
             this.add(new ExtArc.Administration.UI({
             id: 'AdminUI'
             }));
             }
             
             }
             
             }
             }
             */
            ]
        })
        
        
        
        this.createUI();
        
        //Create droptzones for favourites
        
        ret = new ExtArc.Favorites.DropTarget(Ext.get('EastPanel-xcollapsed'), {
            FavoritesContainer: 'EastPanel' + this.LanguageCode
        });
        ret = new ExtArc.Favorites.DropTarget(Ext.get('EastPanel' + this.LanguageCode), {
            FavoritesContainer: 'EastPanel' + this.LanguageCode
        });
        
        Ext.getCmp('EastPanel' + this.LanguageCode).on('favoriteClick', this.openFavorite, this)
        
        //console.log('abou to open')
        if (this.DocID) {
            entry = Ext.getCmp('pnl-' + this.DocID)
            if (!entry) {
                this.tabPanel.add({
                    title: 'Sample Document',
                    xtype: 'clSampleDocument',
                    id: 'pnl-' + this.DocID,
                    unid: this.DocID,
                    server: this.server,
                    path: this.path,
                    closable: true,
                    height: 500,
                    title: 'Uppgifter'
                }).show()
            }
            else {
                entry.show()
            }
        }
        
        
    },
    createUI: function(){
    
        this.viewport = new ExtArc.Layout.Standard({
            id: 'portal-viewport',
            CenterItems: [this.tabPanel],
            TopToolbar: this.Toolbar,
            
            EastPanel: new ExtArc.Favorites.Grid({
                id: 'EastPanel' + this.LanguageCode,// For CSS class background image
                collapsible: true,
                collapsed: true,
                width: 200,
                //region: 'east',
                split: true
            
            
            }),
            WestPanel: new ISales.SearchView({
            
                id: 'WestPanel' + this.LanguageCode, // For CSS class background image
                collapsible: true,
                collapsed: true,
                width: 300,
                region: 'west',
                split: true,
                listeners: {
                    'ItemClicked': this.OpenFromSearchView.createDelegate(this),
                    'expand': function(){
                        Ext.getCmp('searchfield').focus()
                    },
                    scope: this
                }
            })
        
        })
        
        
        Ext.getCmp('LogoContainer').add(new ISales.SettingsToolbar({
            height: 25,
            target: 'isales-tabpanel'
        
        }))
        Ext.getCmp('LogoContainer').doLayout()
    },
    
    OpenInTab: function(node){
    
    
    
    },
    openFavorite: function(rec){
        this.OpenFromSearchView(rec.data.fdDocumentClass, rec.data.fdTargetDocumentKey, '')
        
    },
    
    OpenFromSearchView: function(doctype, unid, el){
        //Ext.get(el).frame()
        
        entry = Ext.getCmp('pnl-' + unid)
        if (!entry) {
            this.tabPanel.add({
                xtype: doctype,
                id: 'pnl-' + unid,
                unid: unid,
                server: this.server,
                path: this.path,
                iconCls: 'Icon_Loading',
                iconClass: doctype,
                closable: true,
                height: 500,
                title: 'Loading...'
            }).show()
        }
        else {
            entry.show()
        }
        
    },
    DirectCall: function(){
        Domino.getKeywordValues(this.server, this.path, 'kwcontactstatus', function(response){
            console.log('result on first call')
            Ext.each(response.data, function(v){
                console.log(v)
            }, this);
            
        })
        
        Domino.getKeywordValues(this.server, this.path, 'kwcontactstatus', function(response){
            console.log('result on second call')
            Ext.each(response.data, function(v){
                console.log(v)
            }, this);
            
        })
        
        Domino.getKeywordValues(this.server, this.path, 'kwcontactstatus', function(response){
            console.log('result on third call')
            Ext.each(response.data, function(v){
                console.log(v)
            }, this);
            
        })
        
        Domino.getKeywordValues(this.server, this.path, 'kwcontactstatus', function(response){
            console.log('result on fourth call')
            Ext.each(response.data, function(v){
                console.log(v)
            }, this);
            
        })
        
        
        
    },
    createDocument: function(node){
        console.log(node.ResponseTo)
        console.log(this.tabPanel.getActiveTab().doctype)
        parentUnid = '';
        if (node.ResponseTo) {
            if (this.tabPanel.getActiveTab().doctype == node.ResponseTo) {
                parentUnid = this.tabPanel.getActiveTab().unid
            }
        }
        console.log(parentUnid)
        this.tabPanel.add({
            xtype: node.doctype,
            id: 'pnl-new-' + node.doctype,
            unid: '',
            parentunid: parentUnid,
            server: this.server,
            path: this.path,
            iconCls: 'Icon_Loading',
            iconClass: node.doctype,
            closable: true,
            height: 500,
            title: 'Loading...'
        }).show()
    }
    
    
    
};
